package com.ht.web360.UserServer.dao;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.ht.web360.base.dto.UserInfo.dto.UserInfoDto;
import com.ht.web360.base.enums.UserType;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDateTime;

@Data
@Schema(description = "用户数据")
@TableName("user_info")
public class UserInfo {
    @Schema(description = "用户id")
    Long id;

    @Schema(description = "用户昵称")
    String nickName;

    @Schema(description = "用户账号")
    String account;

    @Schema(description = "用户密码")
    String password;

    @Schema(description = "用户头像链接")
    String avatar;

    @Schema(description = "用户类型")
    UserType userType;

    @Schema(description = "用户创建时间")
    @TableField(fill = FieldFill.INSERT)
    LocalDateTime createTime;

    @Schema(description = "用户修改时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    LocalDateTime updateTime;

    public UserInfo() {
    }

    public static UserInfo fromDto(UserInfoDto userInfoDto) {
        UserInfo userInfo = new UserInfo();
        if (NumberUtil.isLong(userInfoDto.getId())) {
            userInfo.id = Long.parseLong(userInfoDto.getId());
        }
        userInfo.userType = userInfoDto.getUserType();
        userInfo.avatar = userInfoDto.getAvatar();
        userInfo.account = userInfoDto.getAccount();
        userInfo.nickName = userInfoDto.getNickName();
        return userInfo;
    }

    public UserInfoDto toDto() {
        UserInfoDto dto = new UserInfoDto();
        dto.setId(this.getId().toString());
        dto.setAccount(this.account);
        dto.setAvatar(this.avatar);
        dto.setNickName(this.nickName);
        dto.setUserType(this.userType);
        return dto;
    }
}